<template>
  <div class="register-container">
    <el-card class="register-card">
      <template #header>
        <div class="card-header">
          <h2>注册账号</h2>
          <p class="subtitle">欢迎加入我们</p>
        </div>
      </template>

      <el-form
        ref="registerForm"
        :model="registerData"
        :rules="rules"
        label-position="top"
        class="register-form"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="registerData.username"
            placeholder="请输入用户名"
            :prefix-icon="User"
          />
        </el-form-item>

        <el-form-item label="邮箱" prop="email">
          <el-input
            v-model="registerData.email"
            placeholder="请输入邮箱"
            :prefix-icon="Message"
          />
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input
            v-model="registerData.password"
            type="password"
            placeholder="请输入密码"
            :prefix-icon="Lock"
            show-password
          />
        </el-form-item>

        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input
            v-model="registerData.confirmPassword"
            type="password"
            placeholder="请再次输入密码"
            :prefix-icon="Lock"
            show-password
          />
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            class="register-button"
            :loading="loading"
            @click="handleRegister"
          >
            注册
          </el-button>
        </el-form-item>

        <div class="login-link">
          已有账号？
          <router-link to="/login">立即登录</router-link>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from "vue";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import { User, Message, Lock } from "@element-plus/icons-vue";
import type { FormInstance } from "element-plus";
import { useUserStore } from "@/store/user";

const router = useRouter();
const userStore = useUserStore();
const registerForm = ref<FormInstance>();
const loading = ref(false);

const registerData = reactive({
  username: "",
  email: "",
  password: "",
  confirmPassword: "",
});

// 密码验证规则
const validatePass = (rule: any, value: string, callback: any) => {
  if (value === "") {
    callback(new Error("请输入密码"));
  } else {
    if (registerData.confirmPassword !== "") {
      if (registerForm.value) {
        registerForm.value.validateField("confirmPassword");
      }
    }
    callback();
  }
};

// 确认密码验证规则
const validatePass2 = (rule: any, value: string, callback: any) => {
  if (value === "") {
    callback(new Error("请再次输入密码"));
  } else if (value !== registerData.password) {
    callback(new Error("两次输入密码不一致!"));
  } else {
    callback();
  }
};

const rules = {
  username: [
    { required: true, message: "请输入用户名", trigger: "blur" },
    { min: 3, max: 20, message: "长度在 3 到 20 个字符", trigger: "blur" },
  ],
  email: [
    { required: true, message: "请输入邮箱地址", trigger: "blur" },
    {
      type: "email" as const,
      message: "请输入正确的邮箱地址",
      trigger: "blur",
    },
  ],
  password: [
    { required: true, validator: validatePass, trigger: "blur" },
    { min: 6, max: 20, message: "长度在 6 到 20 个字符", trigger: "blur" },
  ],
  confirmPassword: [
    { required: true, validator: validatePass2, trigger: "blur" },
  ],
};

const handleRegister = async () => {
  if (!registerForm.value) return;

  try {
    await registerForm.value.validate();
    loading.value = true;

    const res = await userStore.registerAction(registerData);
    if (res.status === 201 && res.data?.success) {
      ElMessage.success("注册成功");
      router.push("/login");
    }
  } catch (error) {
    console.error("注册失败:", error);
    ElMessage.error(error || "注册失败");
  } finally {
    loading.value = false;
  }
};
</script>

<style scoped lang="scss">
.register-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: url(@/assets/login.jpeg) center/cover no-repeat;

  .register-card {
    width: 400px;
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    background: rgba(255, 255, 255, 0.9);
    border: 1px solid rgba(255, 255, 255, 0.2);
    box-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.37);

    .card-header {
      text-align: center;

      h2 {
        margin: 0;
        font-size: 24px;
        color: var(--text-color);
        font-weight: 600;
      }

      .subtitle {
        margin: 8px 0 0;
        color: var(--text-color-secondary);
        font-size: 14px;
      }
    }

    .register-form {
      margin-top: 20px;

      .register-button {
        width: 100%;
        padding: 12px 0;
        font-size: 16px;
        background: var(--gradient-primary);
        border: none;
        transition: all 0.3s ease;

        &:hover {
          transform: translateY(-2px);
          box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
        }
      }

      .login-link {
        text-align: center;
        margin-top: 20px;
        color: var(--text-color-secondary);
        font-size: 14px;

        a {
          color: var(--primary-color);
          text-decoration: none;
          font-weight: 500;
          transition: color 0.3s ease;

          &:hover {
            color: var(--primary-color);
            text-decoration: underline;
          }
        }
      }
    }
  }
}

// 响应式样式
@media (max-width: 480px) {
  .register-container {
    padding: 20px;

    .register-card {
      width: 100%;
    }
  }
}
</style>
